/*
 * PFactores.java
 *
 * Created on 18 de febrero de 2007, 07:49 PM
 */

package ig.metrica.ui.matrizcorrelacion;

import ig.metrica.riesgo.FactorRiesgoMatrizCorrelacionMetrica;
import ig.util.MyTableModel;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;


/**
 *
 * @author  jsanchez
 */
public class PFactores extends javax.swing.JPanel implements IPanel{
    public Hashtable<IPanel,Integer> oyentes=new Hashtable<IPanel,Integer>();    
    private ModeloMatrizCorrelacion modelo=null;
    /** Creates new form PFactores */
    public PFactores() {
        initComponents();
        this.TFactor.getTableHeader().setReorderingAllowed(false);
        this.TFactor.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        TFactor.setDefaultRenderer(Object.class, new ig.util.AlignmentRender());        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PMAddFactor = new javax.swing.JPopupMenu();
        MAdd = new javax.swing.JMenuItem();
        MRem = new javax.swing.JMenuItem();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TFactor = new javax.swing.JTable();

        MAdd.setText(org.openide.util.NbBundle.getMessage(PFactores.class, "PFactores.MAdd.text")); // NOI18N
        MAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MAddActionPerformed(evt);
            }
        });
        PMAddFactor.add(MAdd);

        MRem.setText(org.openide.util.NbBundle.getMessage(PFactores.class, "PFactores.MRem.text")); // NOI18N
        MRem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MRemActionPerformed(evt);
            }
        });
        PMAddFactor.add(MRem);

        setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setBackground(new java.awt.Color(255, 255, 255));
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(org.openide.util.NbBundle.getMessage(PFactores.class, "PFactores.jLabel1.text")); // NOI18N
        jLabel1.setOpaque(true);

        TFactor.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        TFactor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                TFactorMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(TFactor);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
            .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void TFactorMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TFactorMouseReleased
        try {
            notificarSiguiente();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_TFactorMouseReleased

    private void MAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MAddActionPerformed
        // TODO add your handling code here:
        Object[][] data = ((MyTableModel) this.TFactor.getModel()).getData();
        Object[][] dataClon = data.clone();
        data = new Object[dataClon.length + 1][2];
        for (int i = 0; i < dataClon.length; i++) {
            data[i][0] = dataClon[i][0];
            data[i][1] = dataClon[i][1];
        }
        data[dataClon.length][0] = "";
        data[dataClon.length][1] = 0.0;
        //((MyTableModel)this.TFactorExtar.getModel()).setData(data);
        this.TFactor.setModel(new MyTableModel(data, new String[]{"Nombre", "Cambio Esperado"}, new boolean[]{true, true}));
        ((MyTableModel)this.TFactor.getModel()).setFactorEnMatrizDeChoque(true);
        this.TFactor.updateUI();
    }//GEN-LAST:event_MAddActionPerformed

    private void MRemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MRemActionPerformed

        Object[][] data = ((MyTableModel) this.TFactor.getModel()).getData();
        int rox = TFactor.getSelectedRow();
        Object[][] dataClon = data.clone();
        data = new Object[dataClon.length - 1][2];
        int j = 0;
        for (int i = 0; i < dataClon.length; i++) {
            if (rox != i) {
                data[j][0] = dataClon[i][0];
                data[j++][1] = dataClon[i][1];
            }
        }
        this.TFactor.setModel(new MyTableModel(data, new String[]{"Nombre", "Cambio Esperado"}, new boolean[]{true, true}));
        ((MyTableModel)this.TFactor.getModel()).setFactorEnMatrizDeChoque(true);
        this.TFactor.updateUI();
    }//GEN-LAST:event_MRemActionPerformed
    Collection c=null;
    public void update(Object obj) {
        //System.out.println("soy factores");
        this.c=(Collection) obj;
        if(c!=null){
            Object[][] data=new Object[c.size()][2];
            int i=0;
            for (FactorRiesgoMatrizCorrelacionMetrica elem : (Collection<FactorRiesgoMatrizCorrelacionMetrica >)c) {
                data[i++][0]=elem;
                data[i-1][1]=String.valueOf(elem.getCambioEsperado());
            }
            this.TFactor.setModel(new MyTableModel(data,new String [] {"Nombre","Cambio Esperado"},new boolean [] {true,true}));
            ((MyTableModel)this.TFactor.getModel()).setFactorEnMatrizDeChoque(true);
            try {
                notificarSiguiente();
            } catch (Exception e) {
            }            
        }
    }
    
    public String getTitle() {
        return this.getName();
    }
    
    public void addOyente(IPanel oyente, Integer integer) {
        oyentes.put(oyente,integer);
    }
    
    public void setTitle(String title) {
        this.setName(title);
    }
    
    public void notificarSiguiente() throws Exception{
//        System.out.println("Notifica factores");
        Enumeration<IPanel> enume=oyentes.keys();
        while(enume.hasMoreElements()){
            IPanel ipanel=enume.nextElement();
            if(oyentes.get(ipanel).equals(0)){
                ipanel.update(c);
                ipanel.notificarSiguiente();
            }
        }        
    }
    
    public void notificarAnterior() throws Exception{
        Enumeration<IPanel> enume=oyentes.keys();
        while(enume.hasMoreElements()){
            IPanel ipanel=enume.nextElement();
            if(oyentes.get(ipanel).equals(1))
                ipanel.update(c);
        }
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem MAdd;
    private javax.swing.JMenuItem MRem;
    private javax.swing.JPopupMenu PMAddFactor;
    private javax.swing.JTable TFactor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    public Collection getData() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
